package Frame.sayoptional;

import FCRendering.FCRMaster;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.events.JumpEvent;
import io.reactivex.schedulers.Schedulers;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import Function.FCMutilPlayCache;

import java.util.ArrayList;
import java.util.List;

import static FCRendering.FCRMaster.*;
//睡觉的选项
public class SleepTypeOptional implements ISayoption{


    @Override
    public void clickWhichOption(SayTextAndOption op) {
        if(FCRMaster.player1.getMoney() >= op.needMoney){
            FCRMaster.player1.setMoney(FCRMaster.player1.getMoney()-op.needMoney);

            FCRMaster.fcv.setOpenSay(false);
            FCRMaster.fcv.setSayNpcName(null);
            FCRMaster.fcv.setSayText(null);

            //立刻黑
            Schedulers.computation().scheduleDirect(new SleepRun(op.getEventId()));
        } else {
            fcv.setSayText(new String[]{"NotEnoughMoney"});
        }
    }


    private class SleepRun implements Runnable{
        String eventId;
        public SleepRun(String eid){
            eventId = eid;
        }
        @Override
        public void run() {
            //主角和队伍全部回满血，但状态不能改
            List<FCPlayerInfo> playerInfos = new ArrayList<>(5);
            playerInfos.add(FCRMaster.player1);
            FCRMaster.player1.setBlood(FCRMaster.player1.getBloodMax());

            gameSave.useTankOfPlayerId(player1, player1.getPlayertank1(), player1.getPlayertankHostId());//下车

            for(FCRPlayerLink link: FCMutilPlayCache.getTeamMembers()){
                FCPlayerInfo info = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                if(info != null) {
                    if(!info.isDeath()) {
                        info.setBlood(info.getBloodMax());
                    }
                    info.setPhysicalnum(info.getPhysical());//恢复体力

                    playerInfos.add(info);
                    gameSave.useTankOfPlayerId(info, info.getPlayertank1(), info.getPlayertankHostId());//下车
                }
            }

            if (FCRMaster.fcv.isSever()) {
                FCRSocketServer.getIn().syncPlayerState(playerInfos);
            } else if (FCRMaster.fcv.isClient()) {
                FCRSocketClient.getIn().syncPlayerState(playerInfos);
            }
            if(eventId != null) {
                //这个类型必须是jump,跳转之前全部人下车
                fcv.setPlayingEvent(eventLib.getEventOfName(eventId));
                JumpEvent jumpEvent = (JumpEvent)fcv.getPlayingEvent();
                sound.setEffectSound(jumpEvent.getMusicnum0());
                fcv.setJumpactionnum(0);
                //TODO 通知队友也要转场了
                if (fcv.isSever()) {
                    FCRSocketServer.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                } else if (fcv.isClient()) {
                    FCRSocketClient.getIn().playerJumpEvent(fcv.getPlayingEvent().getName(), jumpEvent.getDestination());
                }
            }
        }
    }

}
